//
//  WithdrawalVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/28.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "WithdrawalVC.h"
#import "BindBankDoneVC.h"
#import "MyVM.h"
@interface WithdrawalVC ()
@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UILabel *labBank;
@property (weak, nonatomic) IBOutlet UITextField *textMoney;
@property (weak, nonatomic) IBOutlet UILabel *labMoney;
@property (weak, nonatomic) IBOutlet UIButton *btnSubmit;

@end

@implementation WithdrawalVC{
    BankModel   *bankModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"提现";
    self.btnSubmit.layer.masksToBounds=YES;
    self.btnSubmit.layer.cornerRadius=2.0;
    self.labMoney.text=[NSString stringWithFormat:@"可提现金额 %@元",self.model.KeTx];
}

- (IBAction)onclickAll:(UIButton *)sender {
    self.textMoney.text=self.model.KeTx;
}

- (void)setModel:(MyCommissionModel *)model{
    if (_model!=nil) {
        return;
    }
    _model=model;
    WS(weakSelf);
    [[MyVM manage] getBankCardWithUserId:[UserInfoModel share].Id andSuccess:^(id returnValue) {
        if (returnValue) {
            bankModel=returnValue;
            weakSelf.icon.image=[[self getBankWithName:bankModel.BankName] objectForKey:@"img"];
            weakSelf.labBank.text=[NSString stringWithFormat:@"%@\n%@",bankModel.BankName,[NSString stringWithFormat:@"尾号%@ 储蓄卡",[bankModel.BankAccount substringWithRange:NSMakeRange(bankModel.BankAccount.length-4, 4)]]];
        }else{
            [weakSelf.navigationController popViewControllerAnimated:NO];
            weakSelf.bindCard();
        }
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (NSDictionary *)getBankWithName:(NSString *)name{
    for (NSDictionary *dict in [MyVM manage].bankArr) {
        if ([[dict objectForKey:@"name"] isEqualToString:name]) {
            return dict;
        }else if ([[dict objectForKey:@"alias"] isEqualToString:name]){
            return dict;
        }
    }
    return nil;
}

///提现
- (IBAction)onclickSubmit:(UIButton *)sender {
    if ([self.textMoney.text intValue]>[self.model.KeTx intValue]) {
        [SVProgressHUD showErrorWithStatus:@"您的佣金没有这么多"];
        return;
    }else if ([self.textMoney.text intValue]<100){
        [SVProgressHUD showErrorWithStatus:@"单次提现最少100元"];
        return;
    }
    
    WS(weakSelf);
    [[MyVM manage] addWithdrawalRecordWithBankId:bankModel.Id andMoney:self.textMoney.text andSuccess:^(id returnValue) {
        weakSelf.model.KeTx=[NSString stringWithFormat:@"%ld",[weakSelf.model.KeTx integerValue]-[weakSelf.textMoney.text integerValue]];
        weakSelf.labMoney.text=[NSString stringWithFormat:@"可提现金额 %@元",weakSelf.model.KeTx];
        BindBankDoneVC *vc=[[UIStoryboard storyboardWithName:@"My" bundle:nil] instantiateViewControllerWithIdentifier:@"BindBankDoneVC"];
        vc.type=BindBankDoneVCTypeWithdrawal;
        vc.money=weakSelf.textMoney.text;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
